package com.mall.server.controller.pms;

import cn.hutool.core.convert.Convert;
import com.mall.model.domain.PmsProduct;
import com.mall.model.dto.PmsProductDto;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.CommonResult;
import com.mall.model.vo.PmsProductVo;
import com.mall.server.service.pms.PmsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService productService;


    //模糊查询
    @GetMapping("/simpleList")
    public CommonResult simpleList(String keywords) {

        List<PmsProduct> products = productService.simpleList(keywords);
        return CommonResult.success(products);
    }

    //批量修改删除状态
    @PostMapping("/update/deleteStatus")
    public CommonResult updateDeleteStatus(@RequestParam Long[] ids, Integer deleteStatus) {

        productService.updateDeleteStatus(ids, deleteStatus);
        return CommonResult.success(null);
    }

    @PostMapping("/update/newStatus")
    public CommonResult updateNewStatus(@RequestParam Long[] ids, Integer newStatus) {

        productService.updateNewStatus(ids, newStatus);
        return CommonResult.success(null);
    }

    //批量上下架商品
    @PostMapping("/update/publishStatus")
    public CommonResult updatePublishStatus(@RequestParam Long[] ids, Integer publishStatus) {

        productService.updatePublishStatus(ids, publishStatus);
        return CommonResult.success(null);
    }

    //批量推荐商品
    @PostMapping("/update/recommendStatus")
    public CommonResult updateRecommendStatus(@RequestParam Long[] ids, Integer recommendStatus) {

        productService.updateRecommendStatus(ids, recommendStatus);
        return CommonResult.success(null);
    }

    //批量修改审核状态
    @PostMapping("/update/verifyStatus")
    public CommonResult updateVerifyStatus(@RequestParam Long[] ids, Integer verifyStatus) {

        productService.updateVerifyStatus(ids, verifyStatus);
        return CommonResult.success(null);
    }

    //创建商品
    @PostMapping("/create")
    public CommonResult saveProduct(@RequestBody PmsProductDto productDto) {

        productService.saveProduct(productDto);
        return CommonResult.success(null);
    }

    //查询商品
    @GetMapping("/list")
    public CommonResult productList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    Integer brandId, Integer productCategoryId,
                                    Integer publishStatus, Integer verifyStatus,
                                    String keyword, String productSn) {

        CommonPage<PmsProduct> pageInfo = productService.productList(pageNum, pageSize, brandId, productCategoryId,
                publishStatus, verifyStatus, keyword, productSn);
        return CommonResult.success(pageInfo);
    }

    //更改商品
    @PostMapping("/update/{id}")
    public CommonResult updateProduct(@PathVariable("id") Integer id, @RequestBody PmsProductDto productDto) {

        productService.updateProduct(Convert.toLong(id), productDto);
        return CommonResult.success(null);
    }

    //根据商品id获取商品编辑信息
    @GetMapping("/updateInfo/{id}")
    public CommonResult findByIdProductDto(@PathVariable("id") Integer id) {

        PmsProductVo productDto = productService.findByIdProductVo(id);
        return CommonResult.success(productDto);
    }


}
